from rest_framework_jwt.utils import jwt_decode_handler
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from question.models import Reply
from question.serializers.questions import CommentQuestionSerializer, AnswerQuestionSerializer


# 问题评论列表
def get_CommentQuestionSerializer(problem_id):
    instance = Reply.objects.filter(problem_id=problem_id, type=0)

    s = CommentQuestionSerializer(instance=instance, many=True)

    data_list = []
    for data in s.data:
        data_dict = data
        # data_dict['subs'] = ['2111', '222']
        data_list.append(data_dict)

    return data_list


# 问题回答列表
def get_AnswerQuestionSerializer(problem_id):
    instance = Reply.objects.filter(problem_id=problem_id, type=2)

    s = AnswerQuestionSerializer(instance=instance, many=True)

    data_list = []
    for data in s.data:
        data_dict = data
        # data_dict['subs'] = [{'id': 1, "content": '23123', 'createtime': '', "useful_count": 200, 'unuseful_count': 20}]
        data_list.append(data_dict)

    return data_list


def get_user_dict(request):
    # 获取token
    token = request.META.get('HTTP_AUTHORIZATION')[4:]

    # 是否为合法请求
    if token != 'undefined':
        # 获取用户信息
        user_dict = jwt_decode_handler(token=token)
        return user_dict
